---------------------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  C:\Users\user\Documents\work/DNB_survey_consumption/ConGrowth/Replication/Programs/Simulation/distribution_sim
> ulation.log
  log type:  text
 opened on:   3 Dec 2018, 08:22:05

. 
. set seed 1234

. 
. *Number of simulation runs
. local nsim = 10000

. 
. *Number of households in the sample
. local nobs = 3000

. 
. *Median minimum and maximum in the sample
. sca lm = 1400

. sca um = 1750

. 
. *Coefficient by which the household-level minima and maxima can vary from the 
. *respective sample medians
. sca het = 0.1

. assert het>0 & het<1

. 
. *Local of distributions used
. local distlist simtriang uniform

. local list_n: word count `distlist'

. 
. *Setting the number of observations equal to the maximum of the number of households and the number of simulation runs
. local mxn = max(`nobs',`nsim')

. set obs `mxn'
obs was 0, now 10000

. 
. qui gen double id = _n if _n<=`nobs'

. 
. *Generating two household-level coefficients using random draws. 
. *These coefficients will multiply the sample median minimum and maximum to generate household heterogeneity
. *over these two magnitudes
. forvalues k=1/2 {
  2.    
.    local seed = 3617+`k'
  3.    set seed `seed'
  4.    qui gen double coeff`k' = (2*het)*uniform() + (1-het) if id<.
  5. }

. 
. 
. *Calculating the household-level minimum and maximum
. qui gen double lobv = coeff1*lm if id<.

. qui gen double upbv = coeff2*um if id<.

. assert lobv<upbv if id<.

. 
. *Calculating the mid-point between the minimum and maximu
. qui gen mid = (lobv+upbv)/2 if id<.

. 
. forvalues m=1/`list_n' {
  2. 
.    local dist: word `m' of `distlist'
  3. 
.    di in yellow "Distribution is `dist'"
  4. 
.    *Creating a variable that captures the results of the simulation results
.    qui gen double sim_results = .
  5. 
.    *In each simulation run we have a sample of households with a size equal to the parameter `nobs'
.    *and with a distribution that is
.    *equal to the standard uniform times their line number (which is given by _n in Stata). 
.    *Then, for each simulation run we draw from the distribution of each of these `nobs' households 
.    *and compute the cross-sectional variance of the observed outcome
.    forvalues k=1/`nsim' {
  6.      
.       *The observed outcome is equal to the product of uniform times the line number (equal to _n)
.      
.       local seed = 5678 + `k'
  7.       set seed `seed'
  8.       qui gen double uni = uniform() if id<.
  9.       
.       
.       qui gen double outcome = . 
 10.       
.       if "`dist'"=="simtriang" {
 11.       
.          qui replace outcome = lobv + sqrt(uni*(upbv-lobv)*(mid-lobv)) if ///
>              uni>=0 & uni<(mid-lobv)/(upbv-lobv) & id<.
 12.          qui replace outcome = upbv - sqrt((1-uni)*(upbv-lobv)*(upbv-mid)) if ///
>              uni>=(mid-lobv)/(upbv-lobv) & uni<=1 & id<.
 13.       }
 14.       
.       else if "`dist'"=="uniform" {
 15.       
.          qui replace outcome = lobv+(uni*(upbv-lobv)) if id<.
 16.       }
 17.       
.       assert outcome<. if id<.
 18.       
.       *Calculating the std deviation of the observed outcome distribution and recording it in the
.       *appropriate line of the variable denoting the simulation results
.       qui su outcome
 19.       qui replace sim_results = r(sd) if _n==`k'
 20.       drop outcome uni
 21. 
.    *End of loop over simulation runs
.    }
 22. 
. 
.    *Recording the household-level standard-deviations
. 
.    *Simple triangular distribution
.    if "`dist'"=="simtriang" {
 23. 
.       qui gen double sd_ind = sqrt((((lobv)^2)+((upbv)^2)+(mid^2) - ///
>          (upbv*lobv)-(upbv*mid)-(mid*lobv))/18) if id<.
 24.    }
 25.       
.    *Uniform distribution
.    else if "`dist'"=="uniform" {   
 26. 
.       qui gen double sd_ind = sqrt(((upbv-lobv)^2)/12) if id<.
 27.    }
 28.       
.    *We then calculate the average across simulation runs of the standard deviation of the observed outcome
.    *and compare it to the average of the standard deviations of the household-level distributions
.    
.    *Creating a matrix of results
.    mat B_`dist'= J(3,4,.)
 29.    mat colnames B_`dist' = coeff se low_ci95 high_ci95
 30.    mat rownames B_`dist' = `dist'_sim `dist'_sample blank
 31.    
.    *Recording the mean, std. error, and CIs over the simulation draws
.    qui su sim_results
 32.    sca m_simr = r(mean)
 33.    sca se_simr = r(sd)
 34.    sca ci95_low = m_simr - se_simr*invttail(`nsim'-1,0.025)
 35.    sca ci95_high = m_simr + se_simr*invttail(`nsim'-1,0.025)
 36. 
.    mat B_`dist'[1,1] = m_simr
 37.    mat B_`dist'[1,2] = se_simr
 38.    mat B_`dist'[1,3] = ci95_low
 39.    mat B_`dist'[1,4] = ci95_high
 40. 
.    *Recording the mean of the household-level std deviations
.    qui mean sd_ind
 41. 
.    mat A2 = r(table)
 42.    mat b2 = A2["b",1]
 43.    mat se2 = A2["se",1]
 44.    mat ll2 = A2["ll",1]
 45.    mat ul2 = A2["ul",1]
 46. 
.    forvalues l=2/2 {
 47. 
.    mat B_`dist'[`l',1]=b`l'[1,1]
 48.    mat B_`dist'[`l',2]=se`l'[1,1]
 49.    mat B_`dist'[`l',3]=ll`l'[1,1]
 50.    mat B_`dist'[`l',4]=ul`l'[1,1]
 51. 
.    }
 52. 
.    drop sd_ind sim_results
 53. 
.    if `m'==1 {
 54. 
.       mat B=B_`dist'
 55.    }
 56.    else if `m'>1 {
 57. 
.       mat B=(B\B_`dist')
 58.    }
 59. 
.    if `m'==`list_n' {
 60. 
.       mat list B
 61.    }
 62. 
. *End of loop over distributions
. }
Distribution is simtriang
Distribution is uniform

B[6,4]
                  coeff         se   low_ci95  high_ci95
simtriang_~m  99.080678  1.1470697   96.83219  101.32917
simtriang_~e  71.404443  .48824299  70.447118  72.361768
       blank          .          .          .          .
 uniform_sim  125.02152  1.3224918  122.42917  127.61387
uniform_sa~e  100.98113  .69047986   99.62727  102.33499
       blank          .          .          .          .

. 
. *Saving the matrix of results
. mat2txt, mat(B) saving("${projf}/Results/Simulation/Results_distribution_simulation_nobs`nobs'_nsim`nsim'.txt") replace f
> ormat(%15.6f) ///      
>   title("Results for simulation of household-level uncertainty, $S_DATE, $S_TIME)")      
(note: file C:\Users\user\Documents\work/DNB_survey_consumption/ConGrowth/Replication/Results/Simulation/Results_distributi
> on_simulation_nobs3000_nsim10000.txt not found)

. mat drop _all

. 
. drop _all

. 
. *Time stamp
. noisily display "Program started at: ${sttime} of ${stdate}"      /* beginning time stamp */
Program started at: 08:22:05 of 3 Dec 2018

. noisily display "Program finished at: $S_TIME of $S_DATE"       /*ending time stamp*/       
Program finished at: 08:23:04 of  3 Dec 2018

. 
. cap log close
